<!DOCTYPE html>
<html>
<head>
<script src="../../resources/js-test.js"></script>
</head>
<body>
<div id="test" contenteditable>
<p>  hello<br>
world.</p>
<blockquote><div>|<br></div></blockquote>
<em>webkit</em>
</div>
<div id="console"></div>
<script>

if (!window.internals)
    testFailed('This test requires internals object');
else {
    var container = document.getElementById('test');

    function range(startContainer, startOffset, endContainer, endOffset) {
        var range =  document.createRange();
        range.setStart(startContainer, startOffset);
        range.setEnd(endContainer, endOffset);
        return range;
    }

    function locationAndLengthFromRange(range) {
        var location = internals.locationFromRange(container, range);
        var length = internals.lengthFromRange(container, range);
        return [location, length];
    }

    Range.prototype.toArray = function () {
        return [this.startContainer, this.startOffset, this.endContainer, this.endOffset];
    }

    var p = document.querySelector('#test p');
    var div = document.querySelector('#test div');
    var r = internals.rangeFromLocationAndLength(container, 0, 1);
    shouldBe('locationAndLengthFromRange(range(p.firstChild, 0, p.firstChild, 3))', '[0, 1]');
    shouldBe('locationAndLengthFromRange(range(p.firstChild, 1, p.firstChild, 4))', '[0, 2]');
    shouldBe('locationAndLengthFromRange(range(p.firstChild, 2, p.firstChild, 3))', '[0, 1]');
    shouldBe('internals.rangeFromLocationAndLength(container, 0, 1).toArray()', '[p.firstChild, 2, p.firstChild, 3]');

    shouldBe('locationAndLengthFromRange(range(p.firstChild, 2, p.firstChild, 7))', '[0, 5]');
    shouldBe('locationAndLengthFromRange(range(p.firstChild, 2, p, 1))', '[0, 5]');
    shouldBe('locationAndLengthFromRange(range(p.firstChild, 2, p, 2))', '[0, 6]');
    shouldBe('locationAndLengthFromRange(range(p.firstChild, 2, p.childNodes[2], 1))', '[0, 6]');
    shouldBe('internals.rangeFromLocationAndLength(container, 1, 4).toArray()', '[p.firstChild, 3, p.firstChild, 7]');
    shouldBe('internals.rangeFromLocationAndLength(container, 0, 6).toArray()', '[p.firstChild, 2, p.childNodes[2], 1]');

    shouldBe('locationAndLengthFromRange(range(p.childNodes[2], 6, p, 3))', '[11, 1]');
    shouldBe('internals.rangeFromLocationAndLength(container, 11, 2).toArray()', '[p.childNodes[2], 6, div, 0]');

    container.style.display = 'none';
}


</script>
</body>
</html>
